/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.mgrImplclass.methodgenerators;

import com.inspur.edp.bff.designtime.generator.DotNetToJavaStringHelper;
import com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.mgrImplclass.FSMgrImplGenContext;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public class GetVMCodeFromBEMethodGenerator extends ClassMethodGenerator {
    private FSMgrImplGenContext fsMgrImplGenContext;

    public GetVMCodeFromBEMethodGenerator(FSMgrImplGenContext fsMgrImplGenContext) {
        this.fsMgrImplGenContext = fsMgrImplGenContext;
    }

    private static final String parm_beObjectCode = "beObjectCode";

    @Override
    protected Block buildMethodBody() {
        SwitchStatement result = getSwitchBlock();
        getCases(result);

        getDefaultCase(result);
        return buildReturnBlock(result);
    }


    private SwitchStatement getSwitchBlock() {
        SwitchStatement switchStatement = ast.newSwitchStatement();
        switchStatement.setExpression(ast.newSimpleName(parm_beObjectCode));
        return switchStatement;
    }

    private void getDefaultCase(SwitchStatement result) {
        SwitchCase defaultCase =ast.newSwitchCase();
        defaultCase.setExpression(null);
        result.statements().add(defaultCase);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ExpressionUtils.getStringLiteral(ast, ""));

        result.statements().add(returnStatement);
   }

    private void getCases(SwitchStatement result) {

        for (Map.Entry<String, String> item : fsMgrImplGenContext.getMgrGenContext().getFSGeneratorContext().getFsIntermediateTypeInfo()
                .getFSDataInfo().getVmBEObjectMappingDict().entrySet()) {
            if (DotNetToJavaStringHelper.isNullOrEmpty(item.getValue())) {
                continue;
            }
            getCase(result, item);
        }
    }

    private void getCase(SwitchStatement result, java.util.Map.Entry<String, String> pair) {
        SwitchCase caseStatement = ast.newSwitchCase();
        caseStatement.setExpression(ExpressionUtils.getStringLiteral(ast, pair.getValue()));
        result.statements().add(caseStatement);

        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ExpressionUtils.getStringLiteral(ast, pair.getKey()));
        result.statements().add(returnStatement);
    }

    private Block buildReturnBlock(SwitchStatement result) {
        Block block = ast.newBlock();
        block.statements().add(result);
        return block;
    }


    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<Modifier.ModifierKeyword>(Arrays.asList(
                new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PROTECTED_KEYWORD}));
    }

    @Override
    protected boolean getIsOverride() {
        return true;
    }

    @Override
    protected String getMethodName() {
        return "getVMCodeFromBE";
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ParameterInfo paramCode = new ParameterInfo();
        paramCode.setParamType(new TypeInfo(String.class));
        paramCode.setParamName(parm_beObjectCode);
        return new ArrayList(Arrays.asList(new ParameterInfo[]{paramCode}));
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(String.class);
    }
}
